﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace SP_XHWeb.ControlTemplates.SP_XHWeb
{
    public partial class TopPicture : UserControl
    {
        private string DispFormUrl = "/_layouts/SP_XHWeb/DispFormItem.aspx?1=1";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblPicNews.InnerHtml = InitTopPicNews();
            }
        }

        private string InitTopPicNews()
        {
            StringBuilder str = new StringBuilder();
            List<SPListItem> picLists = GetTopPic();
            //标题长度
            int len = 18;

            

            if (picLists != null && picLists.Count > 4)
            {
                picLists = (from emp in picLists orderby emp["创建时间"] descending select emp).Take(4).ToList<SPListItem>();
                str.Append("<ul id='output'>");
                for (int i = 0; i < 4; i++)
                {
                    string linkUrl = DispFormUrl + "&web_url=" + picLists[i].ParentList.ParentWebUrl + "&list_title=" + picLists[i].ParentList.Title + "&item_id=" + picLists[i].ID;
                    str.Append("<li>");
                    str.AppendFormat("<a href=\"javascript:openDialog('{0}','图片新闻')\">", linkUrl);

                    string picurl = string.Empty;
                    if (picLists[i]["Picture"].ToString().Contains("<img"))
                    {
                        string tempic = picLists[i]["Picture"].ToString().Substring(picLists[i]["Picture"].ToString().IndexOf("<img"));
                        picurl = tempic.Substring(0, tempic.IndexOf("/>")+2).Replace("margin:5px", "").Replace("margin: 5px", "");
                    }
                    else
                    {
                        picurl = "/";
                    }

                    str.AppendFormat(picurl);
                    str.AppendFormat("</a>");
                    if (picLists[i].Title.Length > len)
                    {
                        str.AppendFormat("<a class='mya' href=\"javascript:openDialog('{0}','图片新闻')\">{1}</a>", linkUrl, picLists[i].Title.Substring(0, len) + "...");
                    }
                    else
                    {
                        str.AppendFormat("<a class='mya' href=\"javascript:openDialog('{0}','图片新闻')\">{1}</a>", linkUrl, picLists[i].Title);
                    }
                    
                    str.Append("</li>");
                }
                str.Append("</ul>");
                str.Append("<ul id='tabs'>");
                for (int i = 0; i < 4; i++)
                {
                    str.Append("<li>");
                    str.Append("<a href='javascript:;'>");
                    string picurl = string.Empty;
                    if (picLists[i]["Picture"].ToString().Contains("<img"))
                    {
                        string tempic = picLists[i]["Picture"].ToString().Substring(picLists[i]["Picture"].ToString().IndexOf("<img"));
                        picurl = tempic.Substring(0, tempic.IndexOf("/>")+2).Replace("margin:5px", "").Replace("margin: 5px", "");
                    }
                    else
                    {
                        picurl = "/";
                    }

                    str.AppendFormat(picurl);
                    if (picLists[i].Title.Length > len)
                    {
                        str.AppendFormat("<span>{0}</span>", picLists[i].Title.Substring(0, len) + "...");
                    }
                    else
                    {
                        str.AppendFormat("<span>{0}</span>", picLists[i].Title);
                    }
                    str.Append("</a>");
                    str.Append("</li>");
                }
                str.Append("</ul>");
            }
            else
            {
                picLists = (from emp in picLists orderby emp["创建时间"] descending select emp).ToList<SPListItem>();
                str.Append("<ul id='output'>");
                for (int i = 0; i < picLists.Count; i++)
                {
                    string linkUrl = DispFormUrl + "&web_url=" + picLists[i].ParentList.ParentWebUrl + "&list_title=" + picLists[i].ParentList.Title + "&item_id=" + picLists[i].ID;
                    str.Append("<li>");
                    str.AppendFormat("<a href=\"javascript:openDialog('{0}','图片新闻')\">", linkUrl);
                    string picurl = string.Empty;
                    if (picLists[i]["Picture"].ToString().Contains("<img"))
                    {
                        string tempic = picLists[i]["Picture"].ToString().Substring(picLists[i]["Picture"].ToString().IndexOf("<img") );
                        picurl = tempic.Substring(0, tempic.IndexOf("/>")+2).Replace("margin:5px", "").Replace("margin: 5px", "");
                    }
                    else
                    {
                        picurl = "/";
                    }

                    str.AppendFormat(picurl);
                    str.AppendFormat("</a>");
                    if (picLists[i].Title.Length > len)
                    {
                        str.AppendFormat("<a class='mya' href=\"javascript:openDialog('{0}','图片新闻')\">{1}</a>", linkUrl, picLists[i].Title.Substring(0, len) + "...");
                    }
                    else
                    {
                        str.AppendFormat("<a class='mya' href=\"javascript:openDialog('{0}','图片新闻')\">{1}</a>", linkUrl, picLists[i].Title);
                    }
                    str.Append("</li>");
                }
                str.Append("</ul>");
                str.Append("<ul id='tabs'>");
                for (int i = 0; i < picLists.Count; i++)
                {
                    str.Append("<li>");
                    str.Append("<a href='javascript:;'>");
                    string picurl = string.Empty;
                    if (picLists[i]["Picture"].ToString().Contains("<img"))
                    {
                        string tempic = picLists[i]["Picture"].ToString().Substring(picLists[i]["Picture"].ToString().IndexOf("<img"));
                        picurl = tempic.Substring(0, tempic.IndexOf("/>")+2).Replace("margin:5px", "").Replace("margin: 5px", "");
                    }
                    else
                    {
                        picurl = "/";
                    }

                    str.AppendFormat(picurl);
                    if (picLists[i].Title.Length > len)
                    {
                        str.AppendFormat("<span>{0}</span>", picLists[i].Title.Substring(0, len) + "...");
                    }
                    else
                    {
                        str.AppendFormat("<span>{0}</span>", picLists[i].Title);
                    }
                    str.Append("</a>");
                    str.Append("</li>");
                }
                str.Append("</ul>");
            }

            
            return str.ToString();
        }


        //图片新闻需要显示的信息
        private List<SPListItem> GetTopPic()
        {
            List<SPListItem> lists = new List<SPListItem>();
            //  http://
            string listUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            using (SPSite site = new SPSite(listUrl + "/infomationcenter"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists.TryGetList("PictureNews");
                    if (list != null && list.ItemCount != 0)
                    {
                        foreach (SPListItem item in list.Items)
                        {
                            if (item.Fields.ContainsField("首页显示"))
                            {
                                if (item["首页显示"] != null && !string.IsNullOrEmpty(item["首页显示"].ToString()) && item["首页显示"].ToString() == "显示")
                                {
                                    lists.Add(item);
                                }
                            }
                        }
                    }
                }
            }
            return lists;
        }
    }
}
